package com.yk.neon.lib.util.base.redis.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisUtils {
    @Autowired
    private RedisTemplate redisTemplate;

    /**写入缓存*/
    public void set(String key,Object value){
        ValueOperations operations = redisTemplate.opsForValue();
        operations.set(key,value);
    }

    /**写入缓存(时效)*/
    public void set(String key, Object value, Long expireTime, TimeUnit timeUnit){
        this.set(key,value);
        redisTemplate.expire(key,expireTime,timeUnit);
    }

    /**删除缓存*/
    public void del(String key){
        redisTemplate.delete(key);
    }

    /**读缓存*/
    public Object get(String key){
        ValueOperations operations = redisTemplate.opsForValue();
        return operations.get(key);
    }

    /**验证redis是否存在当前key*/
    public Boolean isKey(String key){
        return redisTemplate.hasKey(key);
    }
}

